package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.reservationserver.dao.KnowledgeDao;
import com.ruoyi.reservationserver.entity.Knowledge;
import com.ruoyi.reservationserver.service.KnowledgeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 体育知识表(Knowledge)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:48
 */
@Service("knowledgeService")
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeDao, Knowledge> implements KnowledgeService {
    @Resource
    private KnowledgeDao knowledgeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param knowledgeId 主键
     * @return 实例对象
     */
    @Override
    public Knowledge queryById(Integer knowledgeId) {
        return this.knowledgeDao.queryById(knowledgeId);
    }

    /**
     * 分页查询
     *
     * @param knowledge 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Knowledge> queryByPage(Knowledge knowledge, PageRequest pageRequest) {
        long total = this.knowledgeDao.count(knowledge);
        return new PageImpl<>(this.knowledgeDao.queryAllByLimit(knowledge, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param knowledge 实例对象
     * @return 实例对象
     */
    @Override
    public Knowledge insert(Knowledge knowledge) {
        this.knowledgeDao.insert(knowledge);
        return knowledge;
    }

    /**
     * 修改数据
     *
     * @param knowledge 实例对象
     * @return 实例对象
     */
    @Override
    public Knowledge update(Knowledge knowledge) {
        this.knowledgeDao.update(knowledge);
        return this.queryById(knowledge.getKnowledgeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param knowledgeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer knowledgeId) {
        return this.knowledgeDao.deleteById(knowledgeId) > 0;
    }
}
